/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.actions;

import controler.formbeans.AboutUsBean;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.logic.db.AdminFunctionality;
import model.logic.db.CommonWSFunctionality;
import model.pojo.AboutUs;
import model.pojo.Admins;
import model.pojo.News;
import model.pojo.Products;
import model.pojo.Services;
import org.apache.commons.digester.rss.Channel;
import org.apache.commons.digester.rss.Item;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Maha
 */
public class LoadAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession(true);
        System.out.println(session.isNew());
        //load about us
        CommonWSFunctionality ws = new CommonWSFunctionality();
        Vector<AboutUs> aboutUsMenuAndContents = ws.getAboutUsMenuAndContents();
        ((AboutUsBean) form).setAboutUs(aboutUsMenuAndContents);
        //load news
        Vector<News> news = ws.getNews();
        ((AboutUsBean) form).setNews(news);
        Vector<Products> productsAndContents = ws.getProductsAndContents();
        ((AboutUsBean) form).setProducts(productsAndContents);
        Vector<Services> servicesAndContents = ws.getServicesAndContents();
        ((AboutUsBean) form).setServices(servicesAndContents);


        if ((Admins) session.getAttribute("admin") != null) {
            Admins admin = (Admins) session.getAttribute("admin");
            AdminFunctionality adminfunc = new AdminFunctionality();
            ((AboutUsBean) form).setPriv(adminfunc.getAdminPrivilidges(admin.getAdminId()));

        }
        System.out.println("session loaded");
        create_RssFeed();
        return mapping.findForward("home");
    }

    public void create_RssFeed() {
        Channel newChannel = new Channel();

        CommonWSFunctionality newsBean = new CommonWSFunctionality();
        Vector<News> newsRSS = newsBean.getNews();

        // ResultSet newsRSS = newsbBean.selectAvailableNews();

        newChannel.setCopyright("(c)Globisoft 2001-2012");
        newChannel.setTitle("Globisoft News");
        newChannel.setDescription("");
        newChannel.setLink("http://www.globisoft.com");
        newChannel.setLanguage("en");

        try {
            for (News news : newsRSS) {
                Item item = new Item();
                item.setTitle(news.getNewsTitle());
                String link="http://http://localhost:8080/GlobiSoft/news.do?news=" + news.getNewsShortDescription();
                link=link.replaceAll(" ","+");
                item.setLink(link);
                item.setDescription(news.getNewsDescription());
                System.out.println(news.getNewsDescription());
                newChannel.addItem(item);
            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }

        try {
            String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
            path = path.substring(1, path.indexOf("WEB-INF"));

            String realPath = "/media/maha/Imortant Data/GlobiSoftWork/1- WebSite/GlobiSoft/web/" + "Feed.xml";
            realPath = realPath.replaceAll("%20", " ");
            FileOutputStream fout = new FileOutputStream(realPath);
            newChannel.render(fout);
            fout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
